using Youz.Common.Entity;
using Youz.Common.BLL;
using Youz.CreditRepay.BLL;
using Youz.Common.Helper;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Youz.CreditRepay.Manage.Handlers
{
    public partial class Member_ConfigHandler : IHttpHandler, IRequiresSessionState
    {
        Member_ConfigBLL bll = new Member_ConfigBLL();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string _type = WebHelper.Get("type");
            string result = string.Empty;
            switch (_type)
            {
                case "get":
                    result = Get();
                    break;
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
            }

            context.Response.Write(result);
            context.Response.End();
        }

        private string Get()
        {
            String MemberAccount = WebHelper.Post<String>("MemberAccount");

            return bll.GetJson(MemberAccount);
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("start");
            int pageSize = WebHelper.Post<int>("length");

            string MemberAccount = WebHelper.Post<String>("MemberAccount");
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" 1=1 ");
            if (!string.IsNullOrEmpty(MemberAccount))
            {
                sb.AppendFormat("  and MemberAccount like '%{0}%'", MemberAccount);

            }

            int total = 0;
            DataTable dt = bll.Gets(sb.ToString(), pageIndex, pageSize, out total);

            return Utility.GetJsonByDataTableHaveTime(total, dt);
        }

        private string Save()
        {
            string id = WebHelper.Post("MemberAccount");
            bool isAdd = string.IsNullOrWhiteSpace(id);
            Member_Config model = new Member_Config();

            if (isAdd)
            {
            }
            else
            {
                model = bll.Get(WebHelper.Post<String>("MemberAccount"));
            }

            model.ConfigKey = WebHelper.Post<String>("ConfigKey");
            model.ConfigValue = WebHelper.Post<String>("ConfigValue");
            model.Memo = WebHelper.Post<String>("Memo");
            model.CreateTime = WebHelper.Post<DateTime>("CreateTime");
            model.Modifier = WebHelper.Post<String>("Modifier");
            model.ModifyTime = WebHelper.Post<DateTime>("ModifyTime");


            if (bll.Save(model))
            {
                return Utility.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Save);
            }
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

